/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DBMS;

import java.sql.Connection;
import Clases.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author andreth
 */
public class DBMS {

    static private Connection conexion;

    protected DBMS() {
    }
    static private DBMS instance = null;

    static public DBMS getInstance() {
        if (null == DBMS.instance) {
            DBMS.instance = new DBMS();
        }
        conectar();
        return DBMS.instance;
    }

    public static boolean conectar() {
        try {
            Class.forName("org.postgresql.Driver");
            conexion = DriverManager.getConnection(
                    "jdbc:postgresql://localhost:5432/postgres",
                    "postgres",
                    "postgres");
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }

    public Boolean consultarUsuario(Usuario u) {
        String sqlquery = "SELECT * FROM \"PREPAS\".usuario"
                + " WHERE usuario ='" + u.getUsuario() + "' " + " AND "
                + "password ='" + u.getPassword() + "'";
        
            System.out.println(sqlquery);
        try {
            Statement stmt  = conexion.createStatement();
            ResultSet rs = stmt.executeQuery(sqlquery);
            Boolean b = rs.next();
            return b;
        } catch (SQLException ex) {
            Logger.getLogger(DBMS.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    
        public Usuario consultarUsuario2(Usuario u) {
        String sqlquery = "SELECT * FROM \"PREPAS\".usuario"
                + " WHERE usuario ='" + u.getUsuario() + "' ";
            System.out.println(sqlquery);
        try {
            Statement stmt  = conexion.createStatement();
            ResultSet rs = stmt.executeQuery(sqlquery);
            Boolean b = rs.next();
            if (b){
                u.setPassword(rs.getString("password"));
                return u;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBMS.class.getName()).log(Level.SEVERE, null, ex);
        }
        return u;
    }
}
